package cn.e4j.modular.common.controller;

import cn.e4j.config.properties.E4jProperties;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.annotion.MethodDescription;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.util.AppCxt;
import cn.e4j.modular.common.service.impl.QryServiceImpl;
import cn.e4j.modular.common.service.impl.QueryServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 公共开窗控制类
 *
 * @author ganjia
 * @Date 2019/09/06 14:54
 */
@Controller
@RequestMapping("/qry")
public class QryController extends BaseController {
    @Autowired
    QueryServiceImpl queryService;

    @Autowired
    QryServiceImpl qryService;

    /**
     * 跳转页面
     */
    @RequiresPermissions("sys:qry:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/system/qry.html";
    }

    @RequestMapping("/getDataList")
    @ResponseBody
    public ResponseData getDataList(@DtoParam DtoWapper dtoWapper) throws NoSuchMethodException {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        Method method = queryService.getClass().getMethod(inDto.getString("method"),dtoWapper.getClass());
        List<Dto> result = null;
        try {
            result = (List<Dto>)method.invoke(queryService,dtoWapper);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return new PageReData(page, result);
    }

    @RequestMapping("/getQueryList")
    @ResponseBody
    public ResponseData getQueryList(@DtoParam DtoWapper dtoWapper){
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> methods=new ArrayList<>();
        //创建临时表
        String tableName = "##LS" + getCharAndNumr(5);//临时表名
        E4jProperties e4jProperties = E4jProperties.getOWN();
        if(e4jProperties.getDbType().equals("oracle")){
            tableName=tableName.substring(2);
        }
        //switch(AppCxt.getParam("ERP")){
        //    case "T100":
        //        tableName=tableName.substring(2);
        //        break;
        //    default:
        //        tableName=tableName;
        //        break;
        //}
        this.qryService.createTable(tableName);
        Method[] list = QueryServiceImpl.class.getDeclaredMethods();
        for(int i=0;i<list.length;i++){
            try {
                Dto item = Dtos.newDto();
                String description ="";
                String parameter ="";
                if(QueryServiceImpl.class.getMethod(list[i].getName(), DtoWapper.class).getAnnotation(MethodDescription.class)!=null){
                    description = QueryServiceImpl.class.getMethod(list[i].getName(), DtoWapper.class).getAnnotation(MethodDescription.class).description();
                    parameter = QueryServiceImpl.class.getMethod(list[i].getName(), DtoWapper.class).getAnnotation(MethodDescription.class).parameter();
                }
                item.put("METHOD",list[i].getName());
                item.put("DESCRIPTION",description);
                item.put("PARAMETER",parameter);
                methods.add(item);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
        this.qryService.insertLS(methods, tableName);
//        Dto lsQuery = Dtos.newDto();
//        lsQuery.put("tableName", tableName);
        inDto.put("tableName", tableName);
        List<Dto> result = this.qryService.getLsByGroup(page,inDto);

        //switch(AppCxt.getParam("ERP")){
        //    case "T100":
        //        this.qryService.dropTable(tableName);
        //        break;
        //    default:
        //        break;
        //}
        if(e4jProperties.getDbType().equals("oracle")){
            this.qryService.dropTable(tableName);
        }
        return new PageReData(page, result);
    }

    /**
     * java生成随机数字和字母组合
     */
    public static String getCharAndNumr(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            // 输出字母还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 字符串
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 取得大写字母还是小写字母
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (choice + random.nextInt(26));
            } else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }
}
